package yl.hs.bmipfull.service.laboratory.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratory.IProductOverageDao;
import yl.hs.bmipfull.pojo.laboratory.ProductOverageEntity;
import yl.hs.bmipfull.service.laboratory.IProductOverageService;
import yl.hs.bmipfull.service.production.impl.ProductionBaseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  退剩转信息服务层接口实现<br/>
 * 创建时间: 2021年06月23日 11时25分00秒 <br/>
 *
 * @author 吴高丰
 */
@Service
public class ProductOverageServiceImpl extends ProductionBaseService implements IProductOverageService {

    @Autowired
    IProductOverageDao dao;

    @Override
    public List<ProductOverageEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductOverageEntity.class);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),ProductOverageEntity.class);
        return dao.queryPage(pageQuery.getQuery(),filters);
    }

    @Override
    public PageResult<ProductOverageEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductOverageEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<ProductOverageEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),ProductOverageEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public ProductOverageEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveProductOverage(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProductOverageEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new ProductOverageEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ProductOverageEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入退剩转信息信息", Tl.toJson(entity), ProductOverageEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新退剩转信息信息", Tl.toJson(entity), ProductOverageEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, ProductOverageEntity.class);
        }
        return result;
    }

    @Override
    public List<ProductOverageEntity> findByState(String truckSN) {
        return dao.findByState(truckSN);
    }

    @Override
    public Integer updateOverByTruckCode(String truckCode) {
        return dao.updateOverByTruckCode(truckCode);
    }
}